//
//  LWComposeController.m
//  1.28  weibo
//  Created by mac on 15/2/7.
//  Copyright (c) 2015年 mac. All rights reserved.

#import "LWComposeController.h"
#import "LWTextView.h"
#import "SVProgressHUD.h"
#import "AFNetworking.h"
#import "LWAccount.h"
#import "LWTextView.h"
#import "LWTextViewToolBar.h"
#import "LWPhotoSelectViewController.h"
#import "LWNetWorkTools.h"
#import "LWStatuseSendRequest.h"
@interface LWComposeController ()<UITextViewDelegate>
//自定义输入框
@property (nonatomic,weak) LWTextView *textFView;
//自定义工具栏
@property (nonatomic,weak) LWTextViewToolBar *toolBar;
@property (weak, nonatomic) IBOutlet UIView *inputViewContaint;

@property (nonatomic,strong)  LWPhotoSelectViewController *photoSelectViewController;

@end

@implementation LWComposeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.创建发送和取消按钮
    [self setupNav];
    
    //2.创建文本视图
    [self setupInputView];
    //3.创建键盘工具栏
    [self setupToolBar];
}
- (void)setupNav{
    
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"发送微博";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancleClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sendClick)];
}
- (void)setupInputView{
    
    LWTextView *textView = [[LWTextView alloc] init];
    textView.frame = self.view.bounds;
    textView.alwaysBounceVertical = YES;
    textView.delegate = self;
    textView.placegoder = @"分享你想发送的内容...";
    [self.inputViewContaint addSubview:textView];
    self.textFView = textView;
}
- (void)setupToolBar{

    LWTextViewToolBar *toolBar = [[LWTextViewToolBar alloc] init];
    toolBar.backgroundColor = [UIColor greenColor];
    toolBar.frame = CGRectMake(0, self.view.height - 44, self.view.width, 44);
    [self.view addSubview:toolBar];
    self.toolBar = toolBar;
    //监听键盘的弹出和收起
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIKeyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - 监听键盘
- (void)UIKeyboardWillShow:(NSNotification *)note{
   
    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 取出键盘高度
    CGFloat keyboardHeigth = keyboardFrame.size.height;
    
    // 获取动画时间
    NSTimeInterval time = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    NSInteger animationCurve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    [UIView animateWithDuration:time delay:0 options:animationCurve << 16 animations:^{
        self.toolBar.transform = CGAffineTransformMakeTranslation(0, -keyboardHeigth);
    } completion:nil];

}
- (void)UIKeyboardWillHidden:(NSNotification *)note{
    //LWLog(@"%@",note.userInfo);
    NSTimeInterval time = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    NSInteger animationCurve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    // 2.设置工具条的Y值向上移动键盘的高度
    [UIView animateWithDuration:time delay:0 options:animationCurve << 16 animations:^{
        self.toolBar.transform = CGAffineTransformIdentity;
    } completion:nil];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 设置发送按钮默认不能点击
    self.navigationItem.rightBarButtonItem.enabled = self.textFView.text.length >0;
}
#pragma mark - 发送微博
- (void)sendClick{
    [SVProgressHUD showWithStatus:@"正在加载中..." maskType:SVProgressHUDMaskTypeGradient];
    if (self.photoSelectViewController.images.count > 0) {
        [self sendWithImage];
    }else{
        [self sendWithOutImage];
    }
}
- (void)sendWithImage{

       
    // 2.封装请求参数
    LWStatuseSendRequest *parameters = [[LWStatuseSendRequest alloc] init];
    parameters.access_token = [LWAccount accountFromSandBox].access_token;
    parameters.status = self.textFView.text;
    parameters.image = [self.photoSelectViewController.images firstObject];
    [[LWNetWorkTools shareNetWorkTools] sendMessageWithParameters:parameters success:^(LWstatuses *statuse) {
        // 0.关闭键盘
        [self.view endEditing:YES];
        // 1.关闭当前控制器
        [self dismissViewControllerAnimated:YES completion:nil];
        // 2.弹出提示用户
        [SVProgressHUD showSuccessWithStatus:@"发送成功"];
    } failure:^(NSError *error) {
        LWLog(@"%@",error);
        [SVProgressHUD showErrorWithStatus:@"发送失败"];
    }];
}

- (void)sendWithOutImage{
   
    LWStatuseSendRequest *parameters = [[LWStatuseSendRequest alloc] init];
    parameters.access_token = [LWAccount accountFromSandBox].access_token;
    parameters.status = self.textFView.text;
    
    [[LWNetWorkTools shareNetWorkTools] sendMessageWithParameters:parameters success:^(LWstatuses *statuse) {
        
        // 0.关闭键盘
        
        [self.view endEditing:YES];
        // 1.关闭当前控制器
        [self dismissViewControllerAnimated:YES completion:nil];
        // 2.弹出提示用户
        [SVProgressHUD showSuccessWithStatus:@"发送成功"];
    } failure:^(NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:@"发送失败"];
    }];
}
- (void)cancleClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - textView的代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    [self.view endEditing:YES];
}
- (void)textViewDidChange:(UITextView *)textView{

    self.navigationItem.rightBarButtonItem.enabled = textView.text.length > 0;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    self.photoSelectViewController = segue.destinationViewController;
}

@end
